package com.probiz.estore.customer.service.impl;

import java.util.List;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.customer.Address;
import com.probiz.estore.common.model.system.Region;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.customer.dao.AddressDao;
import com.probiz.estore.customer.service.AddressManager;
import com.probiz.estore.system.dao.RegionDao;
import com.probiz.estore.webapp.util.RequestContext;

/**
 * Address Business Delegate (Proxy) implementation to handle communication between web and persistence layer. Implementation of AddressManager interface.
 * Developer introduced interfaces should be declared here. Won't get overwritten.
 */
public class AddressManagerImpl extends GenericManagerImpl<Address> implements AddressManager {

	private AddressDao addressDao;

	private RegionDao regionDao;

	public void setAddressDao(AddressDao dao) {
		this.addressDao = dao;
	}

	public void setRegionDao(RegionDao regionDao) {
		this.regionDao = regionDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.service.AddressManager#getAllByAppuserId(java.lang.Integer)
	 */
	public List<Address> getAllByAppuserId(Integer appuserId) {
		return addressDao.getAllByAppuserId(appuserId);
	}

	/*
	 * @see com.probiz.estore.customer.service.AddressManager#getDefShippingAddr(java.lang.Integer)
	 */
	public Address getDefShippingAddr(Integer appuserId) {
		if (logger.isDebugEnabled()) {
			logger.debug("getDefShippingAddr appuserId=" + appuserId);
		}
		Address address = addressDao.getDefShippingAddr(appuserId);
		if (address == null) {
			List<Address> list = addressDao.getAllByAppuserId(appuserId);
			if (list != null && list.size() > 0) {
				address = list.get(0);
			}
		}

		return address;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.service.AddressManager#updateDefShippingAddr(java.lang.Integer, java.lang.Integer)
	 */
	public Address updateDefShippingAddr(Integer appuserId, Integer addressId) {
		Address address = null;
		if (null != addressId) {
			address = this.getById(addressId);
			if (address != null) {
				Address defaultAddress = addressDao.getDefShippingAddr(RequestContext.getCurrentUserId());
				if (defaultAddress != null) {
					defaultAddress.setIsDefaultShippingAddress(Constants.FLAG_FALSE);
					save(defaultAddress);
				}
				address.setIsDefaultShippingAddress(Constants.FLAG_TRUE);
				save(address);
			}
		} else {
			List<Address> list = addressDao.getAllByAppuserId(appuserId);
			if (list.size() > 0) {
				address = list.get(0);
				address.setIsDefaultShippingAddress(Constants.FLAG_TRUE);
				save(address);
			}
		}
		return address;
	}

	/*
	 * (not Javadoc)
	 * 
	 * @see com.probiz.estore.customer.service.AddressManager#getAllShippingAddressByAppuserId(java.lang.Integer)
	 */
	public List<Address> getAllShippingAddressByAppuserId(Integer appuserId) {
		return addressDao.getAllShippingAddressByAppuserId(appuserId);
	}

	public void saveFully(Address address) {

		if (address.getIsDefaultShippingAddress() == null) {
			address.setIsDefaultShippingAddress((short) 0);
		}
		if (address.getIsDefaultShippingAddress().shortValue() == 1) {
			List addressList = addressDao.getAppuserOtherAddress(RequestContext.getCurrentUserId(), address.getAddressId());
			for (int i = 0; i < addressList.size(); i++) {
				Address tmpAd = (Address) addressList.get(i);
				tmpAd.setIsDefaultShippingAddress((short) 0);
				save(tmpAd);
			}
		}
		Region region = regionDao.getById(address.getRegionId());
		address.setRegionFullName(region.getFullName());
		save(address);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.service.AddressManager#countAddressOfCustomer(java.lang.Integer)
	 */
	public Long countAddressOfCustomer(Integer customerId) {
		return addressDao.countAddressOfCustomer(customerId);
	}

	@Override
	protected void initManager() {
		this.dao = addressDao;
	}

	@Override
	protected void preDelete(Address entity) {

	}

	@Override
	protected void preSave(Address entity) {

	}

}
